<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">

<title>注册</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="css/StyleSheet.css" type="text/css" />
<script src="jquery/jquery-3.1.1.js"></script>
</head>
<body>
	<table cellpadding="0" cellspacing="0" class="regTable regWidth"
		align="center">
		<tr>
			<td height="33" colspan="6" class="mytd">

				<h2 id="logImage" class="contentColor">注册新用户</h2></td>
		</tr>
		<tr>
			<td width="24%" align="center" valign="top" style="height: 26px"
				class="mytd1"><label for="username">用户名：</label></td>
			<td style="height: 26px" class="mytd"><input type="text"
				name="username" id="username" placeholder="请输入用户名" required
				pattern="[0-9a-zA-Z_]{3,15}" /></td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" height="26" align="center" valign="top">
				<label for="pwd">密码：</label></td>
			<td class="mytd"><input type="password" name="pwd" id="pwd"
				placeholder="请输入密码" required pattern="[0-9a-zA-Z]{3,15}" /></td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" height="26" align="center" valign="top">
				<label for="comfirm"> 确认密码：</label></td>
			<td class="mytd"><input type="password" name="comfirm"
				id="comfirm" placeholder="请输入确认密码" required
				pattern="[0-9a-zA-Z]{3,15}" /></td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" align="center" valign="top"
				style="height: 26px"><label for="realname">姓名</label></td>
			<td style="height: 26px" class="mytd"><input type="text"
				name="realname" id="realname" placeholder="请输入姓名" required /></td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" height="26" align="center" valign="top">
				性别：</td>
			<td class="mytd"
				style="color: black;font-size: 12px;padding-left: 10px;"><input
				type="radio" name="rander" id="male" value="1" checked /><label
				for="male">男</label> <input type="radio" name="rander" id="female"
				value="0" /><label for="female">女</label></td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" height="26" align="center" valign="top">
				<label for="email"> Email： </label></td>
			<td class="mytd"><input type="text" name="email" id="email"
				placeholder="请输入Email" required
				pattern="^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$" /></td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" height="26" align="center" valign="top">
				<label for="address"> 地址：</label></td>
			<td class="mytd"><input type="text" name="address" id="address"
				placeholder="请输入地址" required
				pattern="([\u4e00-\u9fa5a-zA-Z]+[0-9]*)" /></td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" height="26" align="center" valign="top">
				<label for="telephone">手机：</label></td>
			<td class="mytd"><input type="text" name="telephone"
				id="telephone" placeholder="请输入手机号" required pattern="^[0-9]*$" />
			</td>
		</tr>
		<tr>
			<td class="mytd1" width="24%" height="26" align="center" valign="top">
				<label for="code">验证码：</label></td>
			<td class="mytd"><input type="text" name="code" id="code"
				placeholder="请输入验证码" /><img alt="单击刷新验证码" src="code"
				onclick="reloadcode(this,'<%=basePath%>')"> <script
					type="text/javascript">
					function reloadcode(obj,base){ 
						var rand=new Date().getTime(); //这里用当前时间作为参数加到url中，是为了使URL发生变化，这样验证码才会动态加载， 
						//只是一个干扰作用，无确实意义 
						obj.src=base+"code?abc="+rand; //其实服务器端是没有abc的字段的。 
					} 
				</script></td>
		</tr>

		<tr>
			<td colspan="2" align="center"><a href="javascript:;"
				onclick="insert()"><img src="Images/az-finish.gif" /> </a></td>
		</tr>
	</table>

	<script type="text/javascript"> 
	 function insert()
	   {	  
	   	  var n=$("#username").val();
	      var p=$("#pwd").val();
	      var m=$("#comfirm").val();
	      var r=$("#realname").val();
	      var s=$('input:radio:checked').val();
	      var e=$("#email").val();
	      var a=$("#address").val();
	      var t=$("#telephone").val();
	      var c=$("#code").val();
	      if(IsNull(n)||IsNull(m)||IsNull(p)||IsNull(r)||IsNull(e)||IsNull(t)||IsNull(a)||IsNull(c))
	      {
	      		alert("请将信息填写完整!");
	      		return; 
	      }
	      if(IsPass(p)){
	      
	      	alert("密码长度应在3-15之间!");
	      	return; 
	      
	      }
	      if(m!=p){
	      	alert("2次输入的密码不一致!");
	      	return;
	      }
	      if(IsEmail(e)){
	      	 alert("邮箱格式不正确!");
	      	 return;
	      }
	      if(IsString(n)){
	      	alert("用户名只能由英文和数字、下划线组成,字符长度应在3-15之间!");
	      	return;
	      }
	      if(address(a)){
			alert("请填写正确的地址");
			return;	      	
	      }
	       if(checkPhone(t)){
	      
	      	 alert("电话号码必须为数字"); 
	      	 return;
	      } 
	      //实现异步请求
	      $.ajax({
			   type: "POST",
			   url: "register",
			   data: {"user.loginname":n,"user.loginpwd":p,"user.gender":s,"user.username":r,
			   "user.mail":e,"user.phone":t,"user.address":a,"code":c},
			   success: function(msg){
			   	 if(msg == "cr")
			   	 {	
			   	 	alert("验证码输入错误,请重新输入!");
			   	 }
			   	 else
			   	 {
			   	 	 if(msg=="ok")
				     {
				        alert( "注册成功！");
				        top.location.href="index.jsp";
				     }
				     else
				     {
				     	if(msg=="exist")
				     		alert( "用户名已存在，请重新输入！");
				     	else
				          	alert( "注册失败，请稍后再试！");
				     }
			   	 }

			   },
			   error:function(r,r2,err)
			   {
			      alert("连接错误:"+err.message);
			   }
			});
	   }
	   	function IsNull(str){    
   		  if(str.length==0){    
       		 return true;       
    	  }
    	  else
    	  	return false;
		} 
		function IsEmail(str)     
		{     
         
        	if(str.length!=0){    
	       		reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;    
	       		if(!reg.test(str)){    
	          		return true; 
	        	} 
		        else{
		        	return false;
		        }   
	       }    
	  } 
	 function IsString(str)     
	 {     
        if(str.length!=0){    
	        reg=/[0-9a-zA-Z_]{3,15}/;     
	        if(!reg.test(str)){    
	            return true;   
	        } 
	        else{
	        	return false;
	        }   
        }    
	 }
	  function IsPass(str)     
	 {     
        if(str.length<3 || str.length>15){    
	      
            return true;
        } 
        else{
        	return false;
        }   
	 } 
	 function address(str){
	 	if(str.length!=0){    
	        reg=/([\u4e00-\u9fa5a-zA-Z]+[0-9]*)/;  
	        if(!reg.test(str)){    
				return true;
	        }
	        else{
	        
	        	return false;
	        }
        }  
	 }   
	 function checkPhone(str){ 
	    
	    reg=/^[0-9]*$/;
	 
	    if(!(reg.test(str))){ 
	        
	        return true; 
	    } 
	    else{
	    	return false;
	    }
	}
</script>
</body>
</html>
